package cn.xuewenbao.edu.service.impl;

import cn.xuewenbao.edu.entity.ColumnData;
import cn.xuewenbao.edu.entity.vo.ContentDataVo;
import cn.xuewenbao.edu.mapper.ColumnDataMapper;
import cn.xuewenbao.edu.service.ColumnDataService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.val;
import org.jeecg.common.api.vo.Result;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * <p>
 * 专栏数据表 服务实现类
 * </p>
 *
 * @author mafayu
 * @since 2021-04-21
 */
@Service
public class ColumnDataServiceImpl extends ServiceImpl<ColumnDataMapper, ColumnData> implements ColumnDataService {

    @Autowired
    private ColumnDataMapper columnDataMapper;

    public void init(String contentId, String columnType) {
        ColumnData columnData = new ColumnData();
        columnData.setContentId(contentId);
        columnData.setColumnType(columnType);
        columnData.setFavoriteCount(0L);
        columnData.setLikeCount(0L);
        columnDataMapper.insert(columnData);
    }

    //根据内容id返回内容数据（点赞，收藏）
    @Override
    public Result<ContentDataVo> getContentData(String contentId) {
        Result<ContentDataVo> result = new Result<>();
        QueryWrapper<ColumnData> wrapper = new QueryWrapper<>();
        wrapper.eq("content_id", contentId);
        ColumnData columnData = baseMapper.selectOne(wrapper);
        if (columnData == null) {
            result.setSuccess(false);
            result.setMessage("不存在此内容");
            return result;
        }
        ContentDataVo contentDataVo = new ContentDataVo();
        BeanUtils.copyProperties(columnData, contentDataVo);
        result.setSuccess(true);
        result.setMessage("查找成功");
        result.setCode(200);
        result.setResult(contentDataVo);
        return result;
    }

    @Override
    public Result<Map<String, Long>> getLikeCount(String userId) {
        Result<Map<String, Long>> result = new Result<>();
        //获取当前日期
        Calendar calendar = Calendar.getInstance();

        //得到前一天
        calendar.add(Calendar.DAY_OF_MONTH, -1);
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
        String lastDay = formatter.format(calendar.getTime());
        String start = lastDay + " 00:00:00";
        String end = lastDay + " 23:59:59";

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

        QueryWrapper<ColumnData> wrapper = new QueryWrapper<>();
        try {
            wrapper.between("gmt_create", sdf.format(sdf.parse(start)), sdf.format(sdf.parse(end)));
            wrapper.eq("content_id", "1");
        } catch (ParseException e) {
            log.error(e.toString());
        }
        List<ColumnData> columnDataList = baseMapper.selectList(wrapper);
        Map<String, Long> map = new HashMap<>();
        for (ColumnData columnData : columnDataList) {
            if (map.containsKey(formatter.format(columnData.getGmtCreate()))) {
                map.put(formatter.format(columnData.getGmtCreate()), map.get(formatter.format(columnData.getGmtCreate())) + columnData.getLikeCount());
            } else {
                map.put(formatter.format(columnData.getGmtCreate()), columnData.getLikeCount());
            }

        }
        result.setResult(map);
        return result;
    }

    @Override
    public Boolean addColumnData(String contentId, String columnType, Long likeCount, Long favoriteCount, Long commentCount, Long viewCount) {
        ColumnData data = new ColumnData();
        data.setContentId(contentId);
        data.setColumnType(columnType);
        data.setLikeCount(likeCount);
        data.setFavoriteCount(favoriteCount);
        data.setCommentCount(commentCount);
        data.setViewCount(viewCount);
        int insert = baseMapper.insert(data);
        return insert > 0;
    }

    @Override
    public Result<Integer> getAllViewCount(String userId) {
        return null;
    }


}
